/*
    Array.reduce()
    
*/ 

const my_reduce = (arr:number[],callback:Function,initValue:number) =>{
    let result = initValue
    for (let i = 0; i < arr.length; i++) {
            result = callback(result,arr[i])
    }
    return result
} 

// console.log(my_reduce([1,2,3,4,5,6,7,8,9,10],(res: number,value:number)=>{
//     return res+value
// },10));

/*
    findindex 
    满足条件返回第一项下标  不满足返回 -1
*/ 


const my_findIndex = (arr:number[],callback:Function) =>{
    let res = null
    for (let i = 0; i < arr.length; i++) {
        res = callback(arr[i],i)
        if (res) {
            return arr[i]
        }
    }
    return -1
}

// console.log(my_findIndex([1,2,3,4,5,6],(item:number)=>item>7));

// 转驼峰 hello world ts  helloWorldTs

const fn = (str:string):string=> {
    let arr = str.split(" ")
    let newStr = arr[0]
    for (let index = 1; index < arr.length; index++) {
        newStr += arr[index][0].toUpperCase()+arr[index].slice(1)
    }
    return newStr
}

// console.log(fn("hello world ts"));

// 3. 封装函数获取目标字符串（由字母和数字组成）中某字符出现的次数
